package com.joyplus.report.business.controller;

import com.alibaba.fastjson.JSON;
import com.joyplus.report.business.domain.MdCampaignSupplement;
import com.joyplus.report.business.qo.MdCampaignSupplementQO;
import com.joyplus.report.business.service.C2SMdCampaignService;
import com.joyplus.report.business.service.OptimizationTaskStatisticsService;
import com.joyplus.report.business.service.S2SMdCampaignService;
import com.joyplus.report.common.utils.DateUitls;
import com.joyplus.report.core.Result;
import com.joyplus.report.core.ResultGenerator;
import com.mongodb.DBObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Controller
 *
 * @author jeff
 * @date 2018-05-31
 */
@RestController
@Api(tags = "")
@RequestMapping("/md/campaign/s2s")
public class S2SMdCampaignController {

  @Resource
  private S2SMdCampaignService s2SMdCampaignService;

  @Resource
  private OptimizationTaskStatisticsService optimizationTaskStatisticsService;


  @GetMapping("/s2sdetail")
  @ApiOperation(value = "查询详情", response = MdCampaignSupplement.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result s2sdetail(@RequestParam String id,@RequestParam(required = false,name = "startDate",defaultValue = "") String startDate,@RequestParam(required = false,name = "endDate",defaultValue = "") String endDate) {

    List taskStatisticsByDate = optimizationTaskStatisticsService
        .getTaskStatisticsByDate(id.split(","), startDate, endDate);
    List s2SRealCampaign = s2SMdCampaignService
        .getS2SRealCampaign(id.split(","), startDate, endDate);
    List fullFreq = optimizationTaskStatisticsService
        .getOptimizationTaskFullFreq(id.split(","));
    List<String> dates = DateUitls.findDates(startDate, endDate);
    Map<Object, Object> objectObjectMap = new HashMap<>();
    objectObjectMap.put("planCampaign", JSON.toJSON(taskStatisticsByDate));
    objectObjectMap.put("c2SRealCampaign", JSON.toJSON(s2SRealCampaign));
    objectObjectMap.put("dates",JSON.toJSON(dates));
    objectObjectMap.put("fullFreq",JSON.toJSON(fullFreq));
    return ResultGenerator.genSuccessResult(objectObjectMap);
  }


  @GetMapping("/getS2SFre")
  @ApiOperation(value = "查询详情", response = MdCampaignSupplement.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result getC2SFre(@RequestParam String id,@RequestParam(required = false,name = "startDate",defaultValue = "") String startDate,@RequestParam(required = false,name = "endDate",defaultValue = "") String endDate) {

    Map<Object, Object> objectObjectMap = new HashMap<>();
    List taskStatisticsByFre = optimizationTaskStatisticsService
        .getTaskStatisticsByFre(id.split(","), startDate);
    return ResultGenerator.genSuccessResult(taskStatisticsByFre);
  }


  @GetMapping("/getS2SHour")
  @ApiOperation(value = "查询详情", response = MdCampaignSupplement.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result getC2SHour(@RequestParam String id,@RequestParam(required = false,name = "startDate",defaultValue = "") String startDate,@RequestParam(required = false,name = "endDate",defaultValue = "") String endDate) {

    Map<Object, Object> objectObjectMap = new HashMap<>();
    List taskStatisticsByHour = optimizationTaskStatisticsService
        .getTaskStatisticsByHour(id.split(","), startDate, endDate);
    List S2SHour = s2SMdCampaignService.getS2SHour(id.split(","), startDate, endDate);
    List<String> dates = DateUitls.findDates(startDate, endDate);
    objectObjectMap.put("dates",dates);
    objectObjectMap.put("optimizationTask",JSON.toJSON(taskStatisticsByHour));
    objectObjectMap.put("c2SHour",S2SHour);
    return ResultGenerator.genSuccessResult(objectObjectMap);
  }

  @GetMapping("/getS2SHourList")
  @ApiOperation(value = "查询详情", response = MdCampaignSupplement.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result getC2SHourList(@RequestParam String id,@RequestParam(required = false,name = "startDate",defaultValue = "") String startDate,@RequestParam(required = false,name = "endDate",defaultValue = "") String endDate) {

    Map<Object, Object> objectObjectMap = new HashMap<>();
    List taskStatisticsByDate = optimizationTaskStatisticsService
        .getTaskStatisticsByDate(id.split(","), startDate, endDate);

    List s2SRealCampaign = s2SMdCampaignService
        .getS2SRealCampaign(id.split(","), startDate, endDate);
    objectObjectMap.put("optimizationTask",JSON.toJSON(taskStatisticsByDate));
    objectObjectMap.put("c2SList",JSON.toJSON(s2SRealCampaign));
    return ResultGenerator.genSuccessResult(objectObjectMap);
  }




  @GetMapping("/getS2SLocation")
  @ApiOperation(value = "查询详情", response = MdCampaignSupplement.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result getC2SLocation(@RequestParam String id,@RequestParam(required = false,name = "startDate",defaultValue = "") String startDate,@RequestParam(required = false,name = "endDate",defaultValue = "") String endDate) {
    HashMap<String, Object> map = new HashMap<>();
    List taskStatisticsByLocation = optimizationTaskStatisticsService
        .getTaskStatisticsByLocation(id.split(","), startDate, endDate);
    List taskStatisticsByLocationAndDate = optimizationTaskStatisticsService
        .getTaskStatisticsByLocationAndDate(id.split(","), startDate, endDate);
    MdCampaignSupplement byId = s2SMdCampaignService.getById(Long.parseLong(id));
    List fullFreq = optimizationTaskStatisticsService
        .getOptimizationTaskFullFreq(id.split(","));
    map.put("mapData",taskStatisticsByLocationAndDate);
    map.put("campaign",byId);
    map.put("data",taskStatisticsByLocation);
    map.put("fullFre",fullFreq);
    return ResultGenerator.genSuccessResult(map);
  }



  @GetMapping("/listPage")
  @ApiOperation(value = "分页查询", response = MdCampaignSupplement.class, notes = "分页查询")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "pageSize", value = "每页显示数量", paramType = "query", dataType = "int"),
      @ApiImplicitParam(required = true, name = "pageNum", value = "第几页", paramType = "query", dataType = "int"),
      @ApiImplicitParam(required = false, name = "campaignName", value = "搜索key", paramType = "query", dataType = "String")
  })
  public Result listPage(MdCampaignSupplementQO mdCampaignSupplementQO) {
   /* PageInfo<MdCampaignSupplement> p = mdCampaignSupplementService.findPage(mdCampaignSupplementQO);*/
    return ResultGenerator.genSuccessResult(null);
  }


  @GetMapping("/list")
  @ApiOperation(value = "分页查询", response = MdCampaignSupplement.class, notes = "分页查询")
  public Result list(MdCampaignSupplementQO mdCampaignSupplementQO) {
    List<MdCampaignSupplement> p = s2SMdCampaignService.findS2SList();
    return ResultGenerator.genSuccessResult(p);
  }

  @GetMapping("/getGeoListByProvince")
  @ApiOperation(value = "查询详情", response = MdCampaignSupplement.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "long"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = true, name = "province", value = "省份", paramType = "query", dataType = "String")
  })
  public Result getGeoListByProvince(@RequestParam String id,
      @RequestParam(required = false, name = "startDate", defaultValue = "") String startDate,
      @RequestParam(required = false, name = "endDate", defaultValue = "") String endDate,
      @RequestParam String province) {

    List geoListByProvince = optimizationTaskStatisticsService
        .getTaskStatisticsByProvince(id.split(","), startDate, endDate, province);

    List taskStatisticsByLocationAndDate = optimizationTaskStatisticsService
        .getTaskStatisticsByLocationAndDate(id.split(","), startDate, endDate);
    new HashMap<String,Object>();
    return ResultGenerator.genSuccessResult(geoListByProvince);
  }


}
